﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using CarLoanSystem.DAL;

namespace CarLoanSystem
{
    public partial class panelLoan : UserControl
    {
        private static EmployeeItem empItem;
        private static string mode;
        public panelLoan()
        {
            InitializeComponent();
            this.gvLoanInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        public void setEmployee(EmployeeItem item)
        {
            empItem = item;
        }

        public void setMode(string item)
        {
            mode = item;
        }

        public void refreshGridview() 
        {
            //if (empItem.EMPLOYEE_POSITION.Equals("s"))
            //{
                this.gvLoanInfo.DataSource = LoadLoan();
            //}
            //else if (empItem.EMPLOYEE_POSITION.Equals("h"))
            //{
            //    this.gvLoanInfo.DataSource = LoadLoanHead();
            //}
        }

        private void genGridview()
        {
            // gen Grideview
            gvLoanInfo.AutoGenerateColumns = false;

            //Set Columns Count
            gvLoanInfo.ColumnCount = 7;

            //Add Columns
            gvLoanInfo.Columns[1].Name = "LOAN_ID";
            gvLoanInfo.Columns[1].HeaderText = "รหัสการขอกู้";
            gvLoanInfo.Columns[1].DataPropertyName = "LOAN_ID";

            gvLoanInfo.Columns[2].Name = "CUSTOMER_NAME";
            gvLoanInfo.Columns[2].HeaderText = "ชื่อ";
            gvLoanInfo.Columns[2].DataPropertyName = "CUSTOMER_NAME";

            gvLoanInfo.Columns[3].Name = "CUSTOMER_LASTNAME";
            gvLoanInfo.Columns[3].HeaderText = "นามสกุล";
            gvLoanInfo.Columns[3].DataPropertyName = "CUSTOMER_LASTNAME";

            gvLoanInfo.Columns[4].Name = "LOAN_DATE";
            gvLoanInfo.Columns[4].HeaderText = "วันที่ขอกู้";
            gvLoanInfo.Columns[4].DataPropertyName = "LOAN_DATE";

            gvLoanInfo.Columns[5].Name = "PRINCIPLE";
            gvLoanInfo.Columns[5].HeaderText = "จำนวนเงินที่กู้";
            gvLoanInfo.Columns[5].DataPropertyName = "PRINCIPLE";

            gvLoanInfo.Columns[6].Name = "DEBT_MONTH_REST";
            gvLoanInfo.Columns[6].HeaderText = "จำนวนเงินที่เหลือ";
            gvLoanInfo.Columns[6].DataPropertyName = "DEBT_MONTH_REST";
            gvLoanInfo.Columns[6].Width = 120;
            // end gen gridview
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                FrmLoanDetail frmLoanDetail = new FrmLoanDetail(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()), mode, empItem);
                frmLoanDetail.ShowDialog();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //if(empItem.EMPLOYEE_POSITION.Equals("s"))
            //{
                this.gvLoanInfo.DataSource = LoadLoan();
            //}
            //else if (empItem.EMPLOYEE_POSITION.Equals("h"))
            //{
            //    this.gvLoanInfo.DataSource = LoadLoanHead();
            //}
        }

        private DataTable LoadLoan() 
        {
            DataTable dt = LoanInfoDAL.LoadAllLoan(this.txtLoanId.Text, this.txtName.Text, this.txtLastName.Text, empItem.EMPLOYEE_ID, mode);
            return dt;
        }

        //private DataTable LoadLoanHead() 
        //{
        //    DataTable dt = LoanInfoDAL.LoadAllLoanHead(this.txtLoanId.Text, this.txtName.Text, this.txtLastName.Text, empItem.EMPLOYEE_ID);
        //    return dt;
        //}
    }
}
